package com.matt.service.impl;


import com.matt.domain.CheckReportDetail;
import com.matt.domain.Children;
import com.matt.foundation.constant.DefecationRecordConstant;
import com.matt.foundation.utils.DateUtil;
import com.matt.repository.CheckReportDetailRepository;
import com.matt.service.*;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;






@Service
@Slf4j
public class CheckReportServiceImpl implements CheckReportDetailService {
	
	@Autowired
    private CheckReportDetailRepository checkReportDetailRepository;
	
	@Autowired
	private ChildrenService childrenService;
	
	@Autowired
    UploadService uploadService;


	@Override
	public CheckReportDetail findByCreateTimeAndChildId(String childId) {
		
	 	Date now = new Date();
        String currentDay = DateUtil.millisToDateTime(now.getTime(), "yyyy-MM-dd");
        Map<String, Date> map = DateUtil.getCurrentDayBeginAndEnd(currentDay);
        Date begin = map.get("beg");
        Date end = map.get("end");
	
        CheckReportDetail cr =  checkReportDetailRepository.findByChildIdAndCreateTimeBetween(childId,begin, end);
	        
		return cr;
	}


	@Override
	public CheckReportDetail addCheckReport(String staffId, String childId, String checkResult) {

		CheckReportDetail cr = new CheckReportDetail();
		Children children = childrenService.getById(childId);
		
		cr.setChildId(childId);
		cr.setCreated(staffId);
		cr.setChildName(children.getName());
		cr.setDeleted(DefecationRecordConstant.DELETED);
		cr.setCreateTime(new Date());
		cr.setCheckResult(checkResult);
		
		return checkReportDetailRepository.save(cr);
	}

	
    
}
